#!/bin/bash

GET_SOURCE=1
TMPDIR=$(mktemp -d)
MAVEN_BINDIR="${HOME}/maven-3.9.9-bin-$(date +%F-%H-%M-%S)"

function disable_module() {
	local module=$1
	local pomfile=$2
	local moduledir

	if [ -z "$pomfile" ] || [ ! -f $pomfile ]; then
		pomfile=pom.xml
	fi
	moduledir=$(cd $(dirname $pomfile); pwd)

	if [ -z "$module" ] || [ ! -d "$moduledir/$module" ]; then
		echo "$(date +'%F %T.%N') warning: module directory of $module not found"
		return 0
	fi

	sed -i "/<module>$module/d" $pomfile
	rm -rf $moduledir/$module
}

function enable_module() {
	local module=$1

	awk -v mod="$module" '/<\/modules>/{print "    <module>" mod "</module>"}1' pom.xml > pom.xml.tmp
	cp -apf pom.xml.tmp pom.xml
}

function pre_build() {
	local name="$1"
	local json_name
	local version
	local source_url
	local tarpkg

	json_name=$(echo $name | tr '-' '_')
	version="$(cat maven-packages.json | jq .${json_name}.version | tr '"' '\0')"
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	tarpkg="${source_url##*/}"

	echo "source_url: [$source_url]"
	if [ -z "$version" ] || [ -z "$source_url" ] || [ "$source_url" = "null" ]; then
		echo "$(date +'%F %T.%N') error: failed to get version or sourcel url for $name"
		exit 1
	fi

	if [ "$GET_SOURCE" = "1" ]; then
		echo $tarpkg
		[ -f ./$tarpkg ] && rm -fv ./$tarpkg
		echo $source_url | xargs wget
		if [ $? -ne 0 ]; then
			echo "$(date +'%F %T.%N') error: failed to download source for $name"
			exit 1
		fi
	fi

	if [ ! -f $tarpkg ]; then
		echo "$(date +'%F %T.%N') error: tar pacakge [$tarpkg] not found"
		exit 1
	fi

	source_dir=$(tar -tf $tarpkg | head -n 1 | awk -F'/' '{print $1}')
	if [ -z "$source_dir" ]; then
		echo "$(date +'%F %T.%N') error: failed to get source dir for $name"
		exit 1
	fi

	rm -rf ./$source_dir
	tar -zxf $tarpkg
	if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') error: failed to get source for $1"
                exit 1
        fi
}

function generate_maven_bindir() {
	mkdir -p $MAVEN_BINDIR

	# bin
	cp  $TMPDIR/bin $MAVEN_BINDIR -arp

	# conf
	cp $TMPDIR/conf $MAVEN_BINDIR -arp

	# boot
	mkdir -p $MAVEN_BINDIR/boot
	for f in $(ls $TMPDIR/boot/*.jar | sed 's/.*\///g'); do
		jarfile=$(find . -name $f -type f | grep target )
		cp -apv $jarfile $MAVEN_BINDIR/boot
	done

	# lib
	mkdir -p $MAVEN_BINDIR/lib
	cp $TMPDIR/lib/jansi-native $MAVEN_BINDIR/lib -arp
	cp $TMPDIR/lib/ext $MAVEN_BINDIR/lib -arp
	for f in $(ls $TMPDIR/lib/*.jar | sed 's/.*\///g'); do
		jarfile=$(find . -name $f -type f | grep target )
		echo "-------------------- [$f]"
		echo "-------------------- [$jarfile]"
		cp -apv $jarfile $MAVEN_BINDIR/lib
	done
}

function build_maven() {
	local name="maven"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	# disable_module "apache-maven"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn -DdistributionTargetDir=$TMPDIR clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_maven_resolver() {
	local name="maven-resolver"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "maven-resolver-named-locks-hazelcast"
	disable_module "maven-resolver-named-locks-redisson"
	disable_module "maven-resolver-test-util"
	disable_module "maven-resolver-transport-classpath"
	disable_module "maven-resolver-supplier"
	disable_module "maven-resolver-demos"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_maven_shared_utils() {
	local name="maven-shared-utils"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_guice() {
	local name="guice"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "bom"
	disable_module "extensions"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_aopalliance() {
	local name="aopalliance"
	local json_name
	local source_url
	local source_dir=$name

	if [ ! -f aopalliance-1.0.tar.gz ]; then
		return 0
	fi

       # cvs -d:pserver:anonymous@aopalliance.cvs.sourceforge.net:/cvsroot/aopalliance login
       # password empty
       # cvs -z3 -d:pserver:anonymous@aopalliance.cvs.sourceforge.net:/cvsroot/aopalliance export -r HEAD aopalliance
	rm -rf ./$source_dir
	tar zxvf aopalliance-1.0.tar.gz
	pushd $source_dir

	# build
	ant -Dbuild.sysclasspath=only jar javadoc -Dant.build.javac.source=1.8 -Dant.build.javac.target=1.8
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	mkdir target
	cp build/aopalliance.jar target/aopalliance-1.0.jar
	popd
}

function build_guava() {
	local name="guava"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "guava-bom"
	disable_module "guava-gwt"
	disable_module "guava-testlib"
	disable_module "guava-tests"

	# enable module
	enable_module "futures/failureaccess"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_javax_inject() {
	local name="javax-inject"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	./build.sh
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	mkdir target
	mv ./build/maven/javax.inject-1.jar target
	popd
}

function build_javax_annotation() {
	local name="javax-annotation"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_utils() {
	local name="plexus-utils"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_classworlds() {
	local name="plexus-classworlds"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_sec_dispatcher() {
	local name="plexus-sec-dispatcher"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_cipher() {
	local name="plexus-cipher"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_interpolation() {
	local name="plexus-interpolation"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_slf4j() {
	local name="slf4j"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir

	sed -i 's/tasks>/target>/g' slf4j-api/pom.xml
	sed -i "s/tasks>/target>/g" integration/pom.xml

	# disable module
	disable_module "slf4j-simple"
	disable_module "slf4j-nop"
	disable_module "slf4j-jdk14"
	disable_module "slf4j-log4j12"
	disable_module "slf4j-reload4j"
	disable_module "slf4j-jcl"
	disable_module "slf4j-android"
	disable_module "slf4j-ext"
	disable_module "log4j-over-slf4j"
	disable_module "jul-to-slf4j"
	disable_module "osgi-over-slf4j"
	disable_module "integration"
	disable_module "slf4j-site"
	disable_module "slf4j-migrator"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Drequired.jdk.version=8
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_sisu() {
	local name="sisu"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "org.eclipse.sisu.inject.extender"
	disable_module "org.eclipse.sisu.plexus.extender"
        disable_module "org.eclipse.sisu.mojos"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_containers() {
	local name="plexus-containers"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "plexus-component-metadata"
        disable_module "plexus-container-default"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_maven_wagon() {
	local name="maven-wagon"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "wagon-provider-test"
        disable_module "wagon-tcks"

	# disable module in sub-project
	disable_module "wagon-ftp" wagon-providers/pom.xml
	disable_module "wagon-http-lightweight" wagon-providers/pom.xml
	disable_module "wagon-scm" wagon-providers/pom.xml
	disable_module "wagon-ssh-external" wagon-providers/pom.xml
	disable_module "wagon-ssh-common" wagon-providers/pom.xml
	disable_module "wagon-ssh-common-test" wagon-providers/pom.xml
	disable_module "wagon-ssh" wagon-providers/pom.xml
	disable_module "wagon-webdav-jackrabbit" wagon-providers/pom.xml

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_plexus_xml() {
	local name="plexus-xml"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_common_cli() {
	local name="common-cli"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_httpcomponents_client() {
	local name="httpcomponents-client"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "httpmime"
        disable_module "fluent-hc"
        disable_module "httpclient-cache"
        disable_module "httpclient-win"
        disable_module "httpclient-osgi"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_httpcomponents_core() {
	local name="httpcomponents-core"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module
	disable_module "httpcore-nio"
	disable_module "httpcore-osgi"
	disable_module "httpcore-ab"

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=1.8 -Dmaven.compiler.target=1.8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_common_codec() {
	local name="common-codec"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_jansi() {
	local name="jansi"
	local json_name
	local source_url
	local source_dir

	pre_build $name

	json_name=$(echo $name | tr '-' '_')
	source_url="$(cat maven-packages.json | jq .${json_name}.source_url | tr '"' '\0')"
	source_dir=$(tar -tf ${source_url##*/} | head -n 1 | awk -F'/' '{print $1}')

	pushd $source_dir
	# disable module

	# build
	JAVA_HOME=/usr/lib/jvm/java-17 MAVEN_OPTS="-Xmx512m" mvn clean package -Dmaven.compiler.source=8 -Dmaven.compiler.target=8 -Dgpg.skip=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
        if [ $? -ne 0 ]; then
                echo "$(date +'%F %T.%N') failed to build $name"
                exit 1
        fi
	popd
}

function build_all() {
	build_maven
	build_maven_resolver
	build_maven_shared_utils
	build_guice
	build_aopalliance
	build_guava
	build_javax_inject
	build_javax_annotation
	build_plexus_utils
	build_plexus_classworlds
	build_plexus_sec_dispatcher
	build_plexus_cipher
	build_plexus_interpolation
	build_slf4j
	build_sisu
	build_plexus_containers
	build_maven_wagon
	build_plexus_xml
	build_common_cli
	build_httpcomponents_client
	build_httpcomponents_core
	build_common_codec
	build_jansi
	generate_maven_bindir
}

function main() {
	build_all
	echo "     temp directory: $TMPDIR"
	echo "maven bin directory: $MAVEN_BINDIR"
	echo "Add [$MAVEN_BINDIR] to environment PATH to use mvn!"
}

main

# maven
# 1. mvn bin + jar
# 2. requires: jar + license

# maven-build-requires
# 3. buildrequires: m2 repo
